<?php
include("db_connect.php");
$data = array();

if(isset($_GET['approve'])){
	
	if($_GET['approve'] == "approve"){
		approve();
	}
	else{
		disapprove();
	}
	
}


function approve(){
	$userid = $_GET['itemRequest'];
	$data = explode("," , $userid);
	$y= count($data);
	$timepost = date("M-j-Y / h:i:s A"); 
	$x ='Confirm';
	for($i=0; $i<=$y-1; $i++ ){
		
			$sql = "UPDATE tbltradein SET status= '$x' WHERE id = ".$data[$i]." "; 
			mysql_query($sql);
			sendmessage($data[$i]);
	}
	
	if(mysql_affected_rows() ){
		echo true;
	}
	else{
		echo $sql;
	}
}

function disapprove(){
	$userid = $_GET['itemRequest'];
	$data = explode("," , $userid);
	$y= count($data);
	$x = 'Decline';
	for($i=0; $i<=$y-1; $i++ ){
		
			$sql = "UPDATE tbltradein SET status='".$x."' where id= ".$data[$i]." "; 
			mysql_query($sql);
	}

	if(mysql_affected_rows() ){
		echo " 1 ";
	}
	else{
		echo $sql;
	}
}

function sendmessage($id){
if($id == "Guest"){

}
else{
	$sql = mysql_query("SELECT * FROM tbltradein where addoff='".$id."' ")or die(mysql_error());
	while($row = mysql_fetch_array($sql)){
	$fname = $row['fname'];
	$lname = $row['lname'];
	$to =$row['email'];
	}
$sql2= mysql_query("SELECT * FROM tbl_account where id='1' and type='1' limit 1")or die(mysql_error());
$result = mysql_fetch_assoc($sql2);
$email2 = $result['email'];

$subject ="Basciautosales Tradein Request";

$message ="<html>
<body>
<p>Good Day ".$fname." ".$lname.", </p></br>
<p align='justify'>Basciautosales invites you to our shop to make a negotiation about your requested item to trade
 </p></br></br>
<p>Do not reply to this email or else we will not consider it</p></br>
<p>You can email as at ".$email2."</p>


</body>

</html>"; 


$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= "From: admin@basciautosales.zz.mu\r\nReply-To: ";
// Additional headers


$mail_sent = @mail($to,$subject,$message,$headers);
echo $mail_sent ? "Mail sent" : "Mail failed";	
	

}

}

?>